﻿using MDIDemo.Model;
using MDIDemo.PublicClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_Project_PictureBase : Form_BaseDialog
    {
        public Form_Project_PictureBase()
        {
            InitializeComponent();
        }
        public string mainId;
        public ProjectPictureModel projectPictureModel;
        public void Ini()
        {
            base.SetComponent();
        }
        /// <summary>
        /// 窗体状态
        /// </summary>
        /// <param name="WinType">-1：查看，0：修改，1：增加</param>
        public void SetWinType(int WinType)
        {
            base.WinType = WinType;
        }

        protected override void DisplayComponent(string id)
        {
            base.class_SetMemoEdit.SetMemoEdit(this.memoEdit1, false);

            if (id != null)
            {
                string Sql = string.Format(@"Select id
                , fileName
                , fileStream
                , sortNo
                , remark
                From vou_project_picture
                Where id = '{0}'
                Order by sortNo", id);
                DataTable dataTable = base.class_MySqlDataBase.GetDataTable(Sql);
                if (dataTable != null && dataTable.Rows.Count == 1)
                {
                    DataRow dataRow = dataTable.Rows[0];
                    if (dataRow != null)
                    {
                        this.spinEdit1.Value = Convert.ToInt32(dataRow["sortNo"].ToString());
                        this.buttonEdit1.Text = dataRow["fileName"].ToString();
                        this.memoEdit1.Text = dataRow["remark"].ToString();
                    }
                }
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            projectPictureModel = new ProjectPictureModel();
            if (WinType == 1)
                projectPictureModel.id = Class_Tool.GenerateId();
            else
                projectPictureModel.id = base.id;
            projectPictureModel.mainId = this.mainId;
            projectPictureModel.sortNo = Convert.ToInt32(this.spinEdit1.Value);
            projectPictureModel.fileName = this.buttonEdit1.Text;
            byte[] vs = GetPictureData(this.buttonEdit1.Text);
            projectPictureModel.fileStream = vs;
            projectPictureModel.remark = this.memoEdit1.Text;
            this.DialogResult = DialogResult.OK;
        }
        private byte[] ImageToBytes(string fileName)
        {
            return ImageToBytes(Image.FromFile(fileName));
        }
        private byte[] ImageToBytes(Image image)
        {
            ImageFormat format = image.RawFormat;
            using (MemoryStream ms = new MemoryStream())
            {
                if (format.Equals(ImageFormat.Jpeg))
                {
                    image.Save(ms, ImageFormat.Jpeg);
                }
                else if (format.Equals(ImageFormat.Png))
                {
                    image.Save(ms, ImageFormat.Png);
                }
                else if (format.Equals(ImageFormat.Bmp))
                {
                    image.Save(ms, ImageFormat.Bmp);
                }
                else if (format.Equals(ImageFormat.Gif))
                {
                    image.Save(ms, ImageFormat.Gif);
                }
                else if (format.Equals(ImageFormat.Icon))
                {
                    image.Save(ms, ImageFormat.Icon);
                }
                byte[] buffer = new byte[ms.Length];
                //Image.Save()会改变MemoryStream的Position，需要重新Seek到Begin
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buffer, 0, buffer.Length);
                return buffer;
            }
        }
        private void buttonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            //TODO
            bool OkSign = false;
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "请选择jpg或png图片";
            openFileDialog.Filter = "JPG文件交换格式(*.jpg)|*.jpg|可移植网格图形(*.png)|*.png";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog.FileName;
                if (GetFileSize(GetPictureData(fileName)) < 5 * 1024 * 1024)
                {
                    this.buttonEdit1.Text = fileName;
                    OkSign = true;
                }
            }
            openFileDialog.Dispose();
            if (!OkSign)
                MessageBox.Show("项目名称不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        private double GetFileSize(byte[] vs)
        {
            return vs.Length / 1024 / 1024;
        }
        private byte[] GetFileStream(string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open);
            byte[] imagebytes = new byte[fs.Length];
            BinaryReader br = new BinaryReader(fs);
            imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
            br.Close();
            fs.Close();
            return imagebytes;
        }
        private byte[] GetPictureData(string imagePath)
        {
            FileStream fs = new FileStream(imagePath, FileMode.Open);
            byte[] byteData = new byte[fs.Length];
            fs.Read(byteData, 0, byteData.Length);
            fs.Close();
            return byteData;
        }
    }
}
